//
//  DayFifthViewController.m
//  LockTest
//
//  Created by ZhangBo on 2018/12/4.
//  Copyright © 2018 ZhangBo. All rights reserved.
//

#import "DayFifthViewController.h"
#import <ReactiveObjC.h>
#import <RACReturnSignal.h>

@interface DayFifthViewController ()
@property(weak, nonatomic) IBOutlet UITextField *accountFiled;
@property(weak, nonatomic) IBOutlet UITextField *pwdFiled;
@property(weak, nonatomic) IBOutlet UIButton *loginBtn;
@end

@implementation DayFifthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self test_rac3];
}

#pragma mark -- 忽略

- (void)skipDemo {
    RACSubject *subject = [RACSubject subject];
    //skip: 跳跃几个值
    [[subject skip:2] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

    [subject sendNext:@"1"];
    [subject sendNext:@"2"];
    [subject sendNext:@"3"];
}

- (void)distinDemo {
    //1.创建信号
    RACSubject *subject = [RACSubject subject];
    //忽略掉重复数据
    [[subject distinctUntilChanged] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    //请求回来
    [subject sendNext:@"abc haha hehe"];
    [subject sendNext:@"hehe"];
    [subject sendNext:@"1"];
    [subject sendNext:@"1"];
    [subject sendNext:@"2"];
    [subject sendNext:@"2"];
}

- (void)takeDemo {
    RACSubject *subject = [RACSubject subject];

    //take:指定拿前面的哪几条数据!!(从前往后)
    //takeLast:指定拿前面的哪几条数据!!(从后往前)注意点:一定要写结束!!
    [[subject takeLast:2] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

    [subject sendNext:@"2"];
    [subject sendNext:@"3"];
    [subject sendNext:@"1"];
    [subject sendCompleted];


//    RACSubject * subject = [RACSubject subject];
    //专门做一个标记信号!!
    RACSubject *signal = [RACSubject subject];

    //take:指定拿前面的哪几条数据!!(从前往后)
    //takeLast:指定拿前面的哪几条数据!!(从后往前)注意点:一定要写结束!!
    //takeUntil:直到你的标记信号发送数据的时候结束!!!
    [[subject takeUntil:signal] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

    [subject sendNext:@"2"];

    //[signal sendNext:@".."];
    [signal sendCompleted];//标记信号!!

    [subject sendNext:@"3"];
    [subject sendNext:@"1"];
    [subject sendCompleted];

}

- (void)filterDemo {
    [[_pwdFiled.rac_textSignal filter:^BOOL(NSString *value) {
        //value:源信号的内容
        return [value length] > 5;
        //返回值:就是过滤条件,只有满足这个条件,才能获取到内容
    }] subscribeNext:^(NSString *x) {
        NSLog(@"%@", x);
    }];
}

- (void)test_rac4 {

}

#pragma mark -- 登陆

- (void)test_rac3 {
    //组合
    //reduceBlock参数:根据组合的信号关联的  必须 一一对应!!
    RACSignal *signal = [RACSignal combineLatest:@[_accountFiled.rac_textSignal, _pwdFiled.rac_textSignal] reduce:^id(NSString *account, NSString *pwd) {
        //两个文本框的text是否有值!!
        return @(account.length && pwd.length);
    }];

    //    //订阅组合信号
    //    [signal subscribeNext:^(id   x) {
    //        _loginBtn.enabled = [x boolValue];
    //    }];

    RAC(_loginBtn, enabled) = signal;
}

#pragma mark -- 组合

- (void)zipDemo {
    //zipWith:两个信号压缩!只有当两个信号同时发出信号内容,并且将内容合并成为一个元祖给你

    //创建信号
    RACSubject *signalA = [RACSubject subject];
    RACSubject *signalB = [RACSubject subject];

    //压缩
    RACSignal *zipSignal = [signalA zipWith:signalB];

    //接受数据  和发送顺序无关!!
    [zipSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    //发送数据
    [signalB sendNext:@"B"];
    [signalA sendNext:@"A"];
    [signalB sendNext:@"B1"];
    [signalA sendNext:@"A1"];
    [signalB sendNext:@"B2"];
    [signalA sendNext:@"A2"];

}

- (void)mergeDemo {

    //创建信号
    RACSubject *signalA = [RACSubject subject];
    RACSubject *signalB = [RACSubject subject];
    RACSubject *signalC = [RACSubject subject];


    //组合信号
    //    RACSignal * mergeSignal = [signalA merge:signalB];
    RACSignal *mergeSignal = [RACSignal merge:@[signalA, signalB, signalC]];

    //订阅 -- 根据发送的情况接受数据!!
    [mergeSignal subscribeNext:^(id x) {
        //任意一二信号发送内容就会来这个Block
        NSLog(@"%@", x);
    }];




    //发送数据
    [signalC sendNext:@"数据C"];
    [signalA sendNext:@"数据A"];
    [signalB sendNext:@"数据B"];


}

- (void)thenDemo {
    //创建信号!!
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        NSLog(@"发送请求A");
        //发送数据
        [subscriber sendNext:@"数据A"];
        [subscriber sendCompleted];

        return nil;
    }];

    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        NSLog(@"发送请求B");
        //发送数据
        [subscriber sendNext:@"数据B"];
        [subscriber sendCompleted];
        return nil;
    }];

    //then:忽略掉第一个信号所有的值!!
    RACSignal *thenSignal = [signalA then:^RACSignal * {
        return signalB;
    }];

    //订阅信号
    [thenSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];


}

- (void)concatDemo {
    //组合!!
    //创建信号!!
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        NSLog(@"发送请求A");
        //发送数据
        [subscriber sendNext:@"数据A"];
        //哥么结束了!!
        [subscriber sendCompleted];
        //        [subscriber sendError:nil]; 这哥么不行!

        return nil;
    }];

    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        NSLog(@"发送请求B");
        //发送数据
        [subscriber sendNext:@"数据B"];
        [subscriber sendCompleted];
        return nil;
    }];

    RACSignal *signalC = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        NSLog(@"发送请求C");
        //发送数据
        [subscriber sendNext:@"数据C"];

        return nil;
    }];

    //concat:按顺序组合!!
    //创建组合信号!!
    //    RACSignal * concatSignal = [[signalA concat:signalB] concat:signalC];

    RACSignal *concatSignal = [RACSignal concat:@[signalA, signalB, signalC]];

    //订阅组合信号
    [concatSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];


}

#pragma mark -- 映射

- (void)map_demo2 {

    //flattenMap 一般用于信号中的信号
    RACSubject *signalOfSignal = [RACSubject subject];
    RACSubject *signal = [RACSubject subject];

    //订阅信号
//    [signalOfSignal subscribeNext:^(RACSignal * x) {
//        [x subscribeNext:^(id   x) {
//            NSLog(@"%@",x);
//        }];
//    }];

//    [signalOfSignal.switchToLatest subscribeNext:^(id   x) {
//        NSLog(@"%@",x);
//    }];

    [[signalOfSignal flattenMap:^RACSignal *(id value) {
        return value;
    }] subscribeNext:
        ^(id x) {
            NSLog(@"%@", x);
        }];

//    [bindSignal subscribeNext:^(id   x) {
//        NSLog(@"%@",x);
//    }];

    //发送信号
    [signalOfSignal sendNext:signal];
    [signal sendNext:@"123"];
}

- (void)map_demo1 {
    //创建信号
    RACSubject *subject = [RACSubject subject];
    //绑定
    [[subject map:^id(id value) {
        //返回的数据就是需要处理的数据
        return [NSString stringWithFormat:@"处理数据%@", value];
    }] subscribeNext:^(id x) {
        NSLog(@"label:%@", x);
    }];

    //发送数据
    [subject sendNext:@"123"];
    [subject sendNext:@"321"];

}

- (void)flattenMapDemo {
    //创建信号
    RACSubject *subject = [RACSubject subject];

    //绑定信号
    RACSignal *bindSignal = [subject flattenMap:^RACSignal *(id value) {
        //block:只要源信号发送内容就会调用
        //value:就是源信号发送的内容
        value = [NSString stringWithFormat:@"处理数据:%@", value];

        //返回信号用来包装修改过的内容
        return [RACReturnSignal return:value];
    }];

    //订阅绑定信号
    [bindSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

    //发送数据
    [subject sendNext:@"123"];

}

#pragma mark -- Action

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    [self map_demo1];
}

@end
